import numpy as np

from network_form.utils_network import read_data
from network_form.utils_network import load_all_graphs
data_path = "../data_process/All_Unpack/"
graphs_path = "../network_form/all_graphs.pkl"


#计算一个event的两个member相似度在第i区，他们的response相同的数量在第i个区间的比例（第i个分区的人数就
# 是response相同的member pair和response相同的member pair），只计算相似度大于0的member

if __name__ == "__main__":
    event_list, topic_dict, member_list, group_list = read_data(data_path, rebuild=False)
    all_graphs = load_all_graphs(graphs_path)

    len_x=10
    width_x=1/len_x
    weight_all=0
    cnt_all=0
    for event in all_graphs:
        members= list(event.nodes)
        event_weight_i=np.zeros((len_x,))
        cnt_weight_i=np.zeros((len_x,))
        edges=event.edges
        members_information=event.nodes.data()
        for id1_index in range(len(members)):
            for id2_index in range(id1_index,len(members)):
                id1=members[id1_index]
                id2=members[id2_index]
                if id1==id2:
                    continue
                if edges.__contains__((id1,id2)):
                    weight=2*event[id1][id2]['weight']-1
                else:
                    weight=0
                    continue
                i=int(weight/width_x)
                if i>=len_x:
                    i=len_x-1
                response1=members_information[id1]['response']
                response2=members_information[id2]['response']
                if response1==response2:
                    event_weight_i[i]+=1
                cnt_weight_i[i]+=1

        # print(event_weight_i,cnt_weight_i)
        # if cnt_weight_i==0:
        #     continue
        # event_weight_i=event_weight_i/cnt_weight_i


        weight_all+=event_weight_i
        # cnt_all+=1
        cnt_all+=cnt_weight_i


    sum_cos=weight_all/cnt_all
    print(list(sum_cos))
    # print(np.sum(sum_cos))
